//
//  HomeView.m
//  PetAPP
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "HomeView.h"
#import "SelectionOfCell.h"
#import "NewsOfCell.h"


@interface HomeView () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, copy) void (^block)(CGFloat, CGFloat);

@property (nonatomic, copy) void (^afterBlock)();


@end


@implementation HomeView

- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        CGRect newFrame = (CGRect){.size = frame.size};
        [self createSubViewsWithFrame:newFrame];
        [self createKVOAction];
        
    }
    
    return self;
}
/** 创建首页的内容用collection装载 */
- (void)createSubViewsWithFrame:(CGRect)frame {
    /** 设置显示属性 */
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    /** 每一个item的大小和屏幕一样 */
    flowLayout.itemSize = frame.size;
    /** 横向滚动 */
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    /** item直接的间距为0 */
    flowLayout.minimumLineSpacing = 0;
    
    /** 设置collectionView */
    self.collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
    [self addSubview:self.collectionView];
    [_collectionView release];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.bounces = NO;
    
    /** 注册3种item类型 */
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuseForCell"];
    /** 精选的cell */
    [self.collectionView registerClass:[SelectionOfCell class] forCellWithReuseIdentifier:@"poolOfSelection"];
    /** 最新的cell */
    [self.collectionView registerClass:[NewsOfCell class] forCellWithReuseIdentifier:@"poolOfNews"];
    
    
    
}
#pragma mark - 返回的行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 3;
}
#pragma mark - 返回的样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
        SelectionOfCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfSelection" forIndexPath:indexPath];
        
        
        return cell;
    }else if (indexPath.item == 1){
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseForCell" forIndexPath:indexPath];
        
        cell.backgroundColor = [UIColor blackColor];
        
        return cell;   
    
    }else {
        NewsOfCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfNews" forIndexPath:indexPath];
        
        
        return cell;
        
    }
}



#pragma mark - 创建观察者
- (void)createKVOAction {
    
    [self.collectionView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"sg"];
    
}
#pragma mark ** 实现观察者方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    CGFloat x = [[change objectForKey:@"new"] CGPointValue].x;
    CGFloat index = x / self.frame.size.width;
    CGFloat width = self.frame.size.width / 7;
    self.block(width, index);
    if ((index == 0 || index == 1 || index == 2)) {
        self.afterBlock();
    }
}

#pragma mark ** 实现页面滑动效果(通过block控制红线和segment的选择)
- (void)changePageScrollWith:(void (^)(CGFloat, CGFloat))block ConpleteBlock:(void (^)())afterBlock {
    self.block = block;
    self.afterBlock = afterBlock;
}



- (void)dealloc {

    [_collectionView release];
    [super dealloc];
}



@end
